<?php

class Home  extends PageBase {

		public function onInit($param){
			parent::onInit($param);
		}
		public function onLoad($param){
			parent::onLoad($param);
			$this->Bind();
		}

		public function dgDatos_OnPageIndexChanged($sender,$param){
			$this->dgDatos->CurrentPageIndex	= 	$param->NewPageIndex;
			$pageSize 							= 	$this->dgDatos->PageSize;
			$offset 							= 	$param->NewPageIndex*$pageSize;
			$this->dgDatos->DataSource 			= 	$this->Bind($offset, $pageSize);
			$this->dgDatos->dataBind();
		}

		public function dgDatos_OnPagerCreated($sender,$param){
			$style			=	$param->Pager->DataGrid->getPagerStyle();
			$pageCount		=	$param->Pager->DataGrid->getPageCount();
			$pageIndex		=	$param->Pager->DataGrid->getCurrentPageIndex()+1;
			$maxButtonCount	=	$style->getPageButtonCount();
			$buttonCount	=	(($maxButtonCount > $pageCount) ? $pageCount : $maxButtonCount);
			$startPageIndex	=	1;
			$endPageIndex	=	$buttonCount;

			if($pageIndex > $endPageIndex){
				$startPageIndex	=	((int)(($pageIndex - 1) / $maxButtonCount)) * $maxButtonCount + 1;

				if(($endPageIndex = $startPageIndex + $maxButtonCount - 1) > $pageCount){
					$endPageIndex	=	$pageCount;
				}

				if($endPageIndex - $startPageIndex + 1 < $maxButtonCount){
					if(($startPageIndex = $endPageIndex - $maxButtonCount + 1) < 1){
						$startPageIndex	=	1;
					}
				}
			}
			$param->Pager->Controls->insertAt(0,"P&aacute;gina " . $startPageIndex . " a " . $endPageIndex . " de " . $pageCount . ": ");
		}

		protected function Bind(){
			$id =$this->Idasociado->Text;
			if($id!=""){
				$this->dgDatos->DataSource = CuotasPeer::GetCuotasDetalladasByIdAsociado($id);
				$this->dgDatos->dataBind();
			}

		}

		public function btnBuscar_OnClick($sender, $param){
		$this->Bind(0, $this->dgDatos->PageSize);
		if($this->dgDatos->Items->Count>0){
			$this->lblBusqueda->Text = "";
		}
		else{
			$this->lblBusqueda->Text = "No se han encontrado datos para la consulta especificada.";
		}
	}

		#region Autocomplete Asociado

		public function getDummyDataAsociado($token){
			$data = AsociadosPeer::Autocomplete($token);
			$dataToBind = array();
			foreach($data as $d){
				$asociado=$d->getApellidoNombre()." - DNI:".$d->getDni();
				$dataToBind[] = array(
				'Idasociado' => $d->getIdasociado(),
				'Apellidonombre' => $asociado);
			}

			if(count($dataToBind)==0 ){
				$dataToBind[] = array(
				'Idasociado' => "",
				'Apellidonombre' => "No se han encontrado coincidencias" );
			}
			return $dataToBind;
		}

		public function Asociado_OnSuggest($sender, $param)
		{
			$token = $param->getToken();//Obtiene el Texto de la Apicación
			$sender->DataSource = $this->getDummyDataAsociado($token);
			$sender->dataBind();
		}

		public function Asociado_OnSuggestionSelected($sender, $param)
		{
			$id = $sender->Suggestions->DataKeys[$param->selectedIndex];
			$this->Idasociado->Text=$id;
			$this->SelectionTextAsociado->Text = $sender->Text;
		}

		public function Asociado_OnTextChanged($sender, $param)
		{
			$this->Idasociado->Text="";
			$this->SelectionTextAsociado->Text = "";
		}

		#endregion
}
?>